package com.vk.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.vk.project.model.vo.ChartVO;
import com.vk.vkapicommon.model.entity.UserInterfaceInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName : InterfaceInfoMapper
 * @Description : 用户和接口关系信息Mapper接口
 * @Author : LYQ
 * @Date: 2023-11-23 22:07:58
 */
@Mapper
public interface UserInterfaceInfoMapper extends BaseMapper<UserInterfaceInfo> {

    /**
     * 获取调用关系中TOP接口
     *
     * @param limit 限制
     * @return
     */
    List<UserInterfaceInfo> listTopInvokeInterfaceInfo(int limit);

    /**
     * 根据接口Id删除与该接口相关的用户接口信息
     *
     * @param interfaceInfoId 接口Id
     * @return 返回是否删除成功
     */
    boolean deleteInterfaceInfoByInterfacId(@Param("interfaceInfoId") long interfaceInfoId);

    /**
     * 获取已发放次数
     */
    @Select("SELECT SUM(leftNum) FROM `user_interface_info` WHERE isDelete = 0")
    int getIssuedTimes();

    /**
     * 获取累计调用
     */
    @Select("SELECT SUM(calledNum) FROM `user_interface_info` WHERE isDelete = 0")
    int getAccumulatedCalls();

    /**
     * 获取用户调用次数
     *
     * @return 返回数据
     */
    @Select("SELECT SUM(uii.calledNum) AS value,u.username AS name FROM `user_interface_info` AS uii LEFT JOIN `user` AS u ON uii.userId = u.id GROUP BY u.username")
    List<ChartVO> getNumberOfUserCalls();

    /**
     * 获取各个接口的调用次数
     *
     * @return 返回数据
     */
    @Select("SELECT ii.`name` AS name,SUM(uii.calledNum) AS value FROM `user_interface_info` AS uii LEFT JOIN `interface_info` AS ii ON uii.interfaceInfoId = ii.id GROUP BY ii.`name`")
    List<ChartVO> getNumberOfInterfaceCalls();
}




